# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=glirc
pkgver=2.41
pkgrel=0
pkgdesc="Console IRC client written in Haskell"
url="https://github.com/glguy/irc-core"
arch="aarch64 x86_64" # ghc
license="ISC"
makedepends="
	cabal
	ghc
	ncurses-dev
	openssl-dev
	"
subpackages="$pkgname-doc"
_hsopenssl=7c90fc32bd2539b0de0280e686b9836f301e39aa
source="https://hackage.haskell.org/package/glirc-$pkgver/glirc-$pkgver.tar.gz
	https://github.com/glguy/HsOpenSSL/archive/$_hsopenssl/HsOpenSSL-$_hsopenssl.tar.gz
	cabal.project.freeze
	"

# Directory where cabal files are stored.
export CABAL_DIR="${CABAL_DIR:-"$srcdir/cabal"}"

cabal_update() {
	local repo="hackage.haskell.org"

	# Default config uses HTTP, change it to HTTPS.
	[ -f "$CABAL_DIR"/config ] || {
		cabal user-config init
		cabal user-config update -a \
			"repository $repo {url: https://$repo/}"
	}

	[ -d "$builddir" ] || abuild unpack
	msg "Freezing $pkgname dependencies"

	# Resolve deps and generate fresh cabal.project.freeze with version constraints.
	(
		cd "$builddir" || {
			error 'Is $builddir set correctly?'
			return 1
		}
		cabal v2-update
		cabal v2-freeze --shadow-installed-packages

		mv -v cabal.project.freeze "$startdir"/
	)

	if ! abuild checksum; then
		die "Failed to update checksum, run 'abuild checksum' manually"
	fi
}

prepare() {
	cp -r "$srcdir"/HsOpenSSL-$_hsopenssl "$builddir"/HsOpenSSL

	default_prepare

	cat > "$builddir"/cabal.project <<-'END'
		packages: glirc.cabal HsOpenSSL/HsOpenSSL.cabal
	END

	ln -svf "$srcdir"/cabal.project.freeze "$builddir"/
}

build() {
	cabal v2-update
	cabal v2-build glirc:exes \
		--jobs=${JOBS:-1} \
		--prefix=/usr \
		--docdir=/usr/share/doc/$pkgname \
		--sysconfdir=/etc \
		--ghc-options="-optc=-Wno-incompatible-pointer-types"
	# FIXME: errors are related to code generated by Haskell:
	# https://github.com/haskell-cryptography/HsOpenSSL/issues/88
}

check() {
	cabal test
}

package() {
	install -Dvm755 "$(cabal list-bin $pkgname)" -t "$pkgdir"/usr/bin/
	install -Dvm644 $pkgname.1 -t "$pkgdir"/usr/share/man/man1/
	install -Dvm644 README.md -t "$pkgdir"/usr/share/doc/$pkgname/
	install -Dvm644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
}

sha512sums="
9a5655a84ea79364035e6f7c68091d92ee968636b3ccd3461c673c1eedac3ef02e544e9cde88d9ea8adc660fe7cbdd807d0ddf8d3e1c0f5ce82b3dd41bf3a4d6  glirc-2.41.tar.gz
435c4e3f8d5f73eb2a1d6cb25352dea31d19f8881e5625e12a96a4258de64a8d40caf4d3a79084c2319ffdeb482ea82f0c7ca1137d353bdc258665626723dbd9  HsOpenSSL-7c90fc32bd2539b0de0280e686b9836f301e39aa.tar.gz
1fbd9baa539334941f344ee954bad0ecc67b0aa22d77a24494eca934a0f46380838594d6d3e700f6e5572cbf7452662edeb7dda85a586a13a8c37416743a165d  cabal.project.freeze
"
